//
//  CityObj.m
//
//  Created by lizhenqiang  on 16/4/10
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "CityObj.h"


NSString *const kCityObjId = @"_id";
NSString *const kCityObjShowInNews = @"show_in_news";
NSString *const kCityObjLevel = @"level";
NSString *const kCityObjCountryId = @"country_id";
NSString *const kCityObjCover = @"cover";
NSString *const kCityObjName = @"name";
NSString *const kCityObjPid = @"pid";


@interface CityObj ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation CityObj

@synthesize internalBaseClassIdentifier = _internalBaseClassIdentifier;
@synthesize showInNews = _showInNews;
@synthesize level = _level;
@synthesize countryId = _countryId;
@synthesize cover = _cover;
@synthesize name = _name;
@synthesize pid = _pid;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.internalBaseClassIdentifier = [self objectOrNilForKey:kCityObjId fromDictionary:dict];
            self.showInNews = [[self objectOrNilForKey:kCityObjShowInNews fromDictionary:dict] boolValue];
            self.level = [[self objectOrNilForKey:kCityObjLevel fromDictionary:dict] doubleValue];
            self.countryId = [self objectOrNilForKey:kCityObjCountryId fromDictionary:dict];
            self.cover = [self objectOrNilForKey:kCityObjCover fromDictionary:dict];
            self.name = [self objectOrNilForKey:kCityObjName fromDictionary:dict];
            self.pid = [self objectOrNilForKey:kCityObjPid fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.internalBaseClassIdentifier forKey:kCityObjId];
    [mutableDict setValue:[NSNumber numberWithBool:self.showInNews] forKey:kCityObjShowInNews];
    [mutableDict setValue:[NSNumber numberWithDouble:self.level] forKey:kCityObjLevel];
    [mutableDict setValue:self.countryId forKey:kCityObjCountryId];
    [mutableDict setValue:self.cover forKey:kCityObjCover];
    [mutableDict setValue:self.name forKey:kCityObjName];
    [mutableDict setValue:self.pid forKey:kCityObjPid];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.internalBaseClassIdentifier = [aDecoder decodeObjectForKey:kCityObjId];
    self.showInNews = [aDecoder decodeBoolForKey:kCityObjShowInNews];
    self.level = [aDecoder decodeDoubleForKey:kCityObjLevel];
    self.countryId = [aDecoder decodeObjectForKey:kCityObjCountryId];
    self.cover = [aDecoder decodeObjectForKey:kCityObjCover];
    self.name = [aDecoder decodeObjectForKey:kCityObjName];
    self.pid = [aDecoder decodeObjectForKey:kCityObjPid];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_internalBaseClassIdentifier forKey:kCityObjId];
    [aCoder encodeBool:_showInNews forKey:kCityObjShowInNews];
    [aCoder encodeDouble:_level forKey:kCityObjLevel];
    [aCoder encodeObject:_countryId forKey:kCityObjCountryId];
    [aCoder encodeObject:_cover forKey:kCityObjCover];
    [aCoder encodeObject:_name forKey:kCityObjName];
    [aCoder encodeObject:_pid forKey:kCityObjPid];
}

- (id)copyWithZone:(NSZone *)zone
{
    CityObj *copy = [[CityObj alloc] init];
    
    if (copy) {

        copy.internalBaseClassIdentifier = [self.internalBaseClassIdentifier copyWithZone:zone];
        copy.showInNews = self.showInNews;
        copy.level = self.level;
        copy.countryId = [self.countryId copyWithZone:zone];
        copy.cover = [self.cover copyWithZone:zone];
        copy.name = [self.name copyWithZone:zone];
        copy.pid = [self.pid copyWithZone:zone];
    }
    
    return copy;
}


@end
